package com.bjSoft.regressionTestTool.core.builder;


import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;

import cfg.CFG;

public class RegressionManager {

	private static RegressionManager regressionManager;
	
	
	public RegressionManager() {
		super();
	}
	
	public static RegressionManager getInstance() {
		if (regressionManager == null) {
			regressionManager = new RegressionManager();
		} 
		return regressionManager;
	}

	public IProject[] scan(IProject project, IResourceDelta delta,
			IProgressMonitor monitor, boolean b) {
		IResourceDelta[] deltas = delta.getAffectedChildren();
		searchClassFiles(deltas);	
		return null;
	}
	
	public void searchClassFiles(IResourceDelta[] deltas) {
		for (int i = 0; i < deltas.length; i++) {
			IResourceDelta delta = deltas[i];
			IResource res = delta.getResource();
			if (res.getType() == IResource.FILE) {
				IFile file = (IFile) res;
				if (file.getFileExtension().equalsIgnoreCase("class")) {
					checkClassResource(delta);				
				}				
			} else if (res.getType() == IResource.FOLDER) {
				searchClassFiles(delta.getAffectedChildren());
			}			
		}		
	}

	private void checkClassResource(IResourceDelta resourceDelta) {
		IResource res = resourceDelta.getResource();
		IPath path = res.getProject().getLocation();
		
		switch (resourceDelta.getKind()) {			
			case IResourceDelta.ADDED:
				break;
			case IResourceDelta.CHANGED:
				try {
					String className = res.getName();
					className = className.substring(0, className.length() - 6);
					String classLocation = res.getLocation().toOSString();
					classLocation = classLocation.substring(0, classLocation.length() - 6);
					String packagePath = res.getFullPath().toPortableString();
					for (int j = 0; j < 3; j++) {
						int index = packagePath.indexOf("/");
						packagePath = packagePath.substring(index + 1, packagePath.length());
					}
					packagePath = packagePath.substring(0, packagePath.length() - 6);
					IFolder folder = res.getProject().getFolder("new_CFGs");
					if (!folder.exists()) {
						folder.create(true, true, null);						
					}
					if (!classLocation.contains("_covCase")) {
						CFG.runCFG(classLocation, path.toOSString() 
								+ "\\new_CFGs\\", packagePath);
						try {
							ResourcesPlugin.getWorkspace().getRoot().
								refreshLocal(IResource.DEPTH_INFINITE, null);
						} catch (CoreException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					//CodeCoverageFacade.runEmma(className, res.getParent().getLocation());
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (CoreException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case IResourceDelta.REMOVED:
				//TODO: Apagar arquivo do CFG.
				break;
			case IResourceDelta.REPLACED:
				break;
			default:
				break;
		}
	}

}
